/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doxy;

import global.DoxyApp;
import global.MyVector;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;

/**
 *
 * @author Rendra
 */
public class FrmSrcList extends javax.swing.JDialog {
    private String choosenFile;

    /**
     * Creates new form FrmSrcList
     */
    public FrmSrcList(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        LoadSrcFiles();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ChooseBtn = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        srcList = new javax.swing.JList();
        CancelBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Pleace choose file ...");

        ChooseBtn.setText("Choose");
        ChooseBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChooseBtnActionPerformed(evt);
            }
        });

        srcList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        srcList.setSelectedIndex(0);
        srcList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                srcListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(srcList);

        CancelBtn.setText("Cancel");
        CancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(ChooseBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(CancelBtn)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ChooseBtn)
                    .addComponent(CancelBtn))
                .addContainerGap())
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void ChooseBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChooseBtnActionPerformed
        // TODO add your handling code here:
        choosenFile = (srcList.getSelectedValue().toString().isEmpty()) ? null : srcList.getSelectedValue().toString();
        if (choosenFile==null) {
            JOptionPane.showMessageDialog(null, "Please choose a file first", "Warning", JOptionPane.WARNING_MESSAGE);
        } else {
            DoxyApp.bridge.setSelectedSrcFile(choosenFile);
            FrmSrcList.this.setVisible(false);
            FrmSrcList.this.dispatchEvent(new WindowEvent(FrmSrcList.this, WindowEvent.WINDOW_CLOSING));
        }
    }//GEN-LAST:event_ChooseBtnActionPerformed

    private void CancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelBtnActionPerformed
        // TODO add your handling code here:
        FrmSrcList.this.setVisible(false);
        FrmSrcList.this.dispatchEvent(new WindowEvent(FrmSrcList.this, WindowEvent.WINDOW_CLOSING));
    }//GEN-LAST:event_CancelBtnActionPerformed

    private void srcListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_srcListMouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount()==2) {
            choosenFile = (srcList.getSelectedValue().toString().isEmpty()) ? null : srcList.getSelectedValue().toString();
            DoxyApp.bridge.setSelectedSrcFile(choosenFile);
            FrmSrcList.this.setVisible(false);
            FrmSrcList.this.dispatchEvent(new WindowEvent(FrmSrcList.this, WindowEvent.WINDOW_CLOSING));
        }
    }//GEN-LAST:event_srcListMouseClicked

    private void LoadSrcFiles() {
        MyVector javaFiles = DoxyApp.bridge.getListSources();
        srcList.setFocusable(true);
        srcList.setSelectionMode(0);
        srcList.setListData(javaFiles);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmSrcList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmSrcList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmSrcList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmSrcList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                FrmSrcList dialog = new FrmSrcList(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelBtn;
    private javax.swing.JButton ChooseBtn;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList srcList;
    // End of variables declaration//GEN-END:variables
}
